<?php

class UserController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/admin/pages'
			// They can be accessed via: index.php?r=admin/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
    
    public function actionView($id=null)
	{
		if ($id && ($model = UserComponent::model()->findByPk($id))) {
            $this->render('view',array(
                'model'=>$model,
            ));
        } else {
            $this->redirect(Yii::app()->baseUrl);
        }
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
        $model = new UserComponent;
        $dataProvider = new CActiveDataProvider('UserComponent', array(
                                                'criteria'=>array(
                                                        'with'=>array('group'),
                                                ),

                                                'pagination'=>array(
                                                    'pageSize'=>2,
                                                ),
                                                'sort'=>array(
                                                    'defaultOrder'=> array('email'=>false),
                                                )
                                                
                                            ));
        $this->render('index',array(
                'dataProvider'=>$dataProvider,
                'model'=>$model
        ));
	}

	/**
	 * Displays the contact page
	 */
	public function actionForm($id=null)
	{
        $model=new UserComponent();
        $this->handleAjaxValidation($model);
        if ($id && !($model = UserComponent::model()->findByPk($id))) {
            $this->redirect(Yii::app()->baseUrl);
        }
        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        if(isset($_POST['UserComponent']))
        {
            $model->attributes=$_POST['UserComponent'];
            if($model->save()) {
                $this->redirect(Yii::app()->baseUrl);
            }
        }

        $group = GroupComponent::getList();

        $this->render('form',array(
            'model'=>$model,
            'groups'=>$group,
            'section'=>'user'
        ));
	}
}